package com.ews.onlineexchange.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.PostLoad;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.util.SystemUUIDUtil;

/**
 * Embeddable 说明：存储资质等级名称、计算用int值
 * 
 * @author admin
 *
 */
@XmlRootElement
@Entity
public class QualificationLevel extends DataEntity implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private QualificationClass qualificationClass;
	private String	name;
	private Integer	value;
	private String levelIndex;
	
	@XmlTransient
	@ManyToOne
	@NotNull
	public QualificationClass getQualificationClass() {
		return qualificationClass;
	}

	public void setQualificationClass(QualificationClass qualificationClass) {
		this.qualificationClass = qualificationClass;
	}

	@NotNull
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@NotNull
	public Integer getValue() {
		return value;
	}

	public void setValue(Integer value) {
		this.value = value;
	}
	
	@Transient
	public String getLevelIndex() {
		return levelIndex;
	}

	public void setLevelIndex(String levelIndex) {
		this.levelIndex = levelIndex;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		if(StringUtils.isBlank(id)) {
			result = prime * result + ((name == null) ? 0 : name.hashCode());
		}
		result = prime * result + ((qualificationClass == null) ? 0 : qualificationClass.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		QualificationLevel other = (QualificationLevel) obj;
		if(StringUtils.isBlank(id)) {
			if (levelIndex != other.levelIndex)
				return false;
		}
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (qualificationClass == null) {
			if (other.qualificationClass != null)
				return false;
		} else if (!qualificationClass.equals(other.qualificationClass))
			return false;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}

	@Transient
	public String getAvailableNames() {
		List<QualificationLevel> lvls = getQualificationClass().getSortedLevelOptions();
		StringBuilder sb = new StringBuilder();
		for (QualificationLevel _lvl : lvls) {
			if (_lvl.getValue() <= this.getValue())
				sb	.append(_lvl.getName())
					.append(",");
//			else
//				break;
		}
		if (sb.length() == 0)
			return null;
		return sb.substring(0, sb.length() - 1);
	}
	@PostLoad
	private void postLoad() {
		setLevelIndex(SystemUUIDUtil.getUUID());
	}
}
